package cn.edu.ctbu.firstdata.controller;

import cn.edu.ctbu.firstdata.domain.Student;
import cn.edu.ctbu.firstdata.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/student")
public class StudentController {

    @Autowired
    StudentService studentService;

    /**
     * 查询所有学生的信息，为list.html设计
     * @param model
     * @return
     */
    @GetMapping("list")
    public String list(Model model){
        List<Student> students = studentService.getAll();
        model.addAttribute("data", students);
        return "student/list";
    }

    /**
     * 渲染ajaxlist.html
     * @return
     */
    @GetMapping("ajaxlist")
    public String ajaxlist(){
        return "student/ajaxlist";
    }

    /**
     * 根据姓名查询学生信息，为list.html设计
     * @param model
     * @param name
     * @return
     */
    @GetMapping(value = "getbyname")
    public String findByName(Model model, @RequestParam String name){
        List<Student> students = studentService.findByName(name);
        model.addAttribute("data", students);
        return "student/list";
    }
}
